<template>
	<div class="system-menu-container">
		<el-card shadow="hover" class="mb10" v-auth="'system:menu:list'">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.menuName" placeholder="请输入菜单名称" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
					<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover">
			<el-button v-auth="'system:menu:add'" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddMenu()">新增</el-button>
			<el-table ref="tableMenuRef" :data="tableData.data" stripe style="width: 100%" row-key="id" :tree-props="{ children: 'children'}">
				<el-table-column label="菜单名称" show-overflow-tooltip min-width="150">
					<template #default="scope">
						<i :class="scope.row.icon"></i>
						<span class="ml10">{{ scope.row.menuName }}</span>
					</template>
				</el-table-column>
				<el-table-column label="菜单类型" show-overflow-tooltip width="100">
					<template #default="scope">
						<span v-if="scope.row.menuType == 'catalog'" class="color-success">目录</span>
						<span v-else-if="scope.row.menuType == 'menu'" class="color-warning">菜单</span>
						<span v-else class="color-primary">按钮</span>
					</template>
				</el-table-column>
				<el-table-column label="路由名称" prop="name" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.menuType == 'catalog'" class="color-success">{{ scope.row.name }}</span>
						<span v-else-if="scope.row.menuType == 'menu'" class="color-warning">{{ scope.row.name }}</span>
						<span v-else class="color-primary">{{ scope.row.name }}</span>
					</template>
				</el-table-column>
				<el-table-column label="组件地址" prop="component" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.menuType == 'catalog'" class="color-success">{{ scope.row.component }}</span>
						<span v-else-if="scope.row.menuType == 'menu'" class="color-warning">{{ scope.row.component }}</span>
						<span v-else class="color-primary">{{ scope.row.component }}</span>
					</template>
				</el-table-column>
				<el-table-column label="权限标识" prop="perms" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.menuType == 'catalog'" class="color-success">{{ scope.row.perms }}</span>
						<span v-else-if="scope.row.menuType == 'menu'" class="color-warning">{{ scope.row.perms }}</span>
						<span v-else class="color-primary">{{ scope.row.perms }}</span>
					</template>
				</el-table-column>
				<el-table-column label="排序" prop="orderNum" width="70" show-overflow-tooltip>

				</el-table-column>
				<el-table-column label="公共" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.isCommon == 'Y'" class="color-primary">是</span>
						<span v-else class="color-info">否</span>
					</template>
				</el-table-column>
				<el-table-column label="隐藏" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.isHide == 'Y'" class="color-primary">是</span>
						<span v-else class="color-info">否</span>
					</template>
				</el-table-column>
				<el-table-column label="缓存" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.isKeepAlive == 'Y'" class="color-primary">是</span>
						<span v-else class="color-info">否</span>
					</template>
				</el-table-column>
				<el-table-column label="固定" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.isAffix == 'Y'" class="color-primary">是</span>
						<span v-else class="color-info">否</span>
					</template>
				</el-table-column>
				<el-table-column label="外链" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.isLink == 'Y'" class="color-primary">是</span>
						<span v-else class="color-info">否</span>
					</template>
				</el-table-column>
				<el-table-column label="内嵌" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.isLink == 'Y' && scope.row.isIframe == 'Y'" class="color-primary">是</span>
						<span v-else class="color-info">否</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="155"
					v-auths="['system:menu:add', 'system:menu:edit', 'system:menu:remove']">
					<template #default="scope">
						<el-button v-auth="'system:menu:add'" type="primary" link @click="onOpenAddMenu(scope.row)">新增</el-button>
						<el-button v-auth="'system:menu:edit'" type="primary" link @click="onOpenEditMenu(scope.row)">修改</el-button>
						<el-button v-auth="'system:menu:remove'" type="primary" link @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<AddMenu ref="addMenuRef" @submit="onTableSearch"/>
		<EditMenu ref="editMenuRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddMenu from '/@/views/system/menu/addMenu.vue';
import EditMenu from '/@/views/system/menu/editMenu.vue';
import { getMenuList, delMenu } from '/@/api/system/menu/index';
import { lineToTreeData } from '/@/utils/commonFunction';
export default {
	name: 'menu',
	components: { AddMenu, EditMenu },
	setup() {
		const tableMenuRef = ref();
		const addMenuRef = ref();
		const editMenuRef = ref();
		const state = reactive({
			search:{
				menuName: undefined
			},
			tableData: {
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params) => {
			getMenuList(params).then((res) => {
				if(res.code == 0){
					state.tableData.data = lineToTreeData(res.data);
					state.tableData.total = res.total;
				}
			});
		}

		//查询
		const onTableSearch = () => {
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.search.menuName = undefined;
			loadTableData();
		}
		// 打开新增菜单弹窗
		const onOpenAddMenu = (row: object) => {
			addMenuRef.value.openDialog(row);
		};
		// 打开编辑菜单弹窗
		const onOpenEditMenu = (row: object) => {
			editMenuRef.value.openDialog(row);
		};
		// 删除当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定删除该条【' + row.menuName + '】菜单信息吗？', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delMenu([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('删除成功');
						onTableSearch();
					}
				});
			}).catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			loadTableData();
		});
		return {
			tableMenuRef,
			addMenuRef,
			editMenuRef,
			onTableSearch,
			onResetSearch,
			onOpenAddMenu,
			onOpenEditMenu,
			onTabelRowDel,
			...toRefs(state),
		};
	},
};
</script>